package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.R;
import com.itheima.reggie.mapper.EmployeeMapper;
import com.itheima.reggie.domian.Employee;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;


import javax.servlet.http.HttpServletRequest;


/**
 * @author 何须浅碧深红色
 * @date 2022/9/11
 */
@Slf4j
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService {
    @Autowired
    private EmployeeService employeeService;
    @Override
    public R<Employee> login(HttpServletRequest request,Employee employee){
        //1,获取密码，进行加密处理
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        //2，根据用户名去查询用户名是否存在
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Employee::getUsername,employee.getUsername());
        Employee one = employeeService.getOne(wrapper);
        if (one==null){
            return R.error("登录失败");
        }
        //3,如果用户名正确就判断密码是不是正确的
        if (!one.getPassword().equals(password)){
            return R.error("登录失败");
        }
        //如果都正确在判断用户是否处于禁用状态
        if(one.getStatus()==0){
            return R.error("该用户已经被禁用");
        }
        //登录成功
        request.getSession().setAttribute("employee",one.getId());
        return R.success(one);
    }

//    @Override
//    public R<String> addOne(HttpServletRequest request, Employee employee) {
//        log.info("新增员工，员工信息：{}",employee.toString());
//
//        //设置初始密码123456，需要进行md5加密处理
//        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
//
//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());
//
//        //3
//        Long empId = (Long) request.getSession().getAttribute("employee");
//
//        employee.setCreateUser(empId);
//        employee.setUpdateUser(empId);
//
//        employeeService.save(employee);
//        return R.success("新增员工成功");
//    }

}
